1. Field of the Invention
This invention relates generally to computer program execution systems, e.g., optimizing compilers, and more specifically, to a system and method for dynamically optimizing running programs to result in specialized code tailored and optimized to a specific execution of the program for improved performance.
2. Discussion of the Prior Art
Some modern programming languages (e.g., Java) support dynamic loading of the application program at run-time. Since the program code is not available until run-time, language implementations and virtual machines dynamically optimize or Just-In-Time compile the program as it runs. One problem of this approach arises when trying to dynamically optimize a program while it runs. After generating the optimized code, the system must somehow translate the state of the running program into a form that allows execution to continue in the optimized code. This translation raises engineering problems for the optimizing compiler; all optimizations and phases of the compiler must account for potential transition, complicating the compiler implementation and potentially inhibiting some optimizations.
As defined herein, an activation is a single execution of a code sequence, such as a procedure or method in a running program. An activation is a dynamic entity; for example, as a program calls and returns from procedures or methods, it creates and destroys activations. An activation may refer to execution of a code sequence in either an interpreted or compiled context. An activation is associated with a program state, which comprises current values of memory locations, variable values, and execution context of the executing code sequence of the activation. The particular instantiation of program state depends on the implementation of the run-time system that executes the code sequence of the activation. For example, in a Java virtual machine, the program state may consist of the values of variables and stack locations in the virtual machine specification, along with a bytecode index identifying the current program counter. Alternatively, an interpreter may define program state in terms of memory locations and variable values used in the internal interpreter implementation. A dynamic binary translation system might use an intermediate representation close to the binary interface for the machine target.
For example, consider an activation representing the execution of a sequence of Java bytecodes. The program state for an activation is a triple  less than PC, S, L greater than : with PC representing the program counter; i.e., the offset of the currently executing bytecode; with S holding the values of the stack, a set of abstract memory locations as defined by the Java Virtual Machine Specification; and, with L holding the values of the local variables, named abstract memory locations as defined by the Java Virtual Machine Specification.
Previous systems such as the Self-93 system and the IBM Mixed-Mode Interpreter JIT support dynamic optimization of procedure activations. However, these systems rely on a method whereby the optimizing compiler generates code for the original source code, and provides extensive mapping information to translate program state into the machine state for the optimized code. These approaches furthermore require tremendous engineering effort into building the optimizing compiler in order to support transition to optimized code at various program points, and, particularly, to enable the optimizing compiler to keep careful track of mappings from original program state to the state of the optimized code.
It would thus be highly desirable to provide an optimizing compiler that may efficiently generate a specialized optimization of a program without the need for modifying the optimizing compiler.
It is an object of the present invention to provide a system for dynamically optimizing running code by generating specialized code tailored and optimized to a specific execution of the program while preserving semantics of original program code.
It is a further object of the present invention to provide a system for dynamically optimizing running code before or during run time.
It is another object of the present invention to provide an apparatus for efficiently generating a specialized optimization of a program without the need for modifying the optimizing compiler.
It is another object of the present invention to provide a system for dynamically optimizing running code by generating specialized code tailored and optimized to a specific execution and including a prologue of instructions for providing program state.
According to the invention, there is provided a system and method for dynamically optimizing a code sequence of a program while executing in a computer system, the method comprising: a) inserting one or more program yield points at which an unoptimized original code sequence may be optimized in the executing program; b) generating a prologue of instructions for setting up program state associated with the unoptimized original code sequence; c) adding the prologue of instructions to an intermediate representation of the original code sequence code for generating a specialized code sequence; and, d) compiling the specialized code sequence with an optimizing compiler for generating a run-time representation of the specialized code sequence, the run-time representation being further optimized for execution on a target computer system.
Advantageously, such a system and method for preserving semantics of original program code by addition of the specialized continuation exploits run-time state to generate better code for improving performance for longer running procedure activations.